<template>
  <div>
    <el-dialog
      v-if="fileVisible"
      :title="$t('batchshipmentrecord')"
      :visible.sync="fileVisible"
      width="900px"
    >
      <div class="container">
        <el-form size="small" inline label-width="100px">
          <el-form-item :label="$t('operationtime')" class="width100">
            <el-date-picker
              v-model="timeVal"
              value-format="yyyy/MM/dd"
              format="yyyy/MM/dd"
              size="small"
              type="daterange"
              placement="bottom-end"
              :placeholder="$t('customtime')"
              style="width: 250px"
              @change="onchangeTime"
            />
          </el-form-item>
          <el-form-item :label="$t('shipmenttype')">
            <el-select
              v-model="tableFrom.type"
              :placeholder="$t('pleaseselect')"
              class="filter-item selWidth mr20"
              clearable
              @change="getList(1)"
            >
              <el-option
                v-for="item in deliveryTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('status1')">
            <el-select
              v-model="tableFrom.status"
              :placeholder="$t('pleaseselect')"
              class="filter-item selWidth mr20"
              clearable
              @change="getList(1)"
            >
              <el-option
                v-for="item in deliveryStatusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div v-loading="loading">
        <el-table
          v-loading="loading"
          :data="tableData.data"
          style="width: 100%"
          size="mini"
          class="table"
          highlight-current-row
        >
          <el-table-column :label="$t('serialno')" min-width="90">
            <template slot-scope="scope">
              <span>{{
                scope.$index + (tableFrom.page - 1) * tableFrom.limit + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('shipmenttype')" min-width="90">
            <template slot-scope="scope">
              <span>{{ scope.row.type | deliveryType }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('operationtime')"
            prop="create_time"
            min-width="180"
          />
          <el-table-column
            :label="$t('numberofdeliveryorders')"
            prop="count"
            min-width="180"
          />
          <el-table-column
            :label="$t('successfulNumber ')"
            prop="success"
            min-width="180"
          />
          <el-table-column :label="$t('state')" min-width="80">
            <template slot-scope="scope">
              <span>{{ scope.row.status | deliveryStatusFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column
            key="8"
            :label="$t('operation')"
            min-width="150"
            fixed="right"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status != -1"
                type="text"
                size="small"
                class="mr10"
                @click="detail(scope.row.import_id)"
                >{{ $t("see") }}</el-button
              >
              <el-button
                v-if="scope.row.status != -1"
                type="text"
                size="small"
                class="mr10"
                @click="downLoad(scope.row.import_id)"
                >{{ $t("generatefiles") }}</el-button
              >
              <el-button
                v-if="scope.row.status == -1"
                type="text"
                size="small"
                class="mr10"
                @click="detail(scope.row.import_id)"
                >{{ $t("viewreason") }}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            :page-sizes="[10, 20, 30]"
            :page-size="tableFrom.limit"
            :current-page="tableFrom.page"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableData.total"
            @size-change="handleSizeChange"
            @current-change="pageChange"
          />
        </div>
      </div>
    </el-dialog>
    <el-dialog
      v-if="dialogVisible"
      title=""
      :visible.sync="dialogVisible"
      width="900px"
    >
      <div v-loading="loading">
        <el-table
          v-loading="loading"
          :data="recordData.data"
          style="width: 100%"
          size="mini"
          class="table"
          highlight-current-row
        >
          <el-table-column :label="$t('serialno')" min-width="90">
            <template slot-scope="scope">
              <span>{{
                scope.$index + (recordFrom.page - 1) * recordFrom.limit + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('ordernumber')"
            prop="order_sn"
            min-width="200"
          />
          <el-table-column
            :label="$t('logisticscompany')"
            prop="delivery_name"
            min-width="80"
          />
          <el-table-column
            :label="$t('logisticsorderno')"
            prop="delivery_id"
            min-width="180"
          />
          <el-table-column
            :label="$t('shipmentstatus')"
            prop="status"
            min-width="80"
          />
          <el-table-column
            :label="$t('abnormalcauses')"
            prop="mark"
            min-width="120"
          />
        </el-table>
        <div class="block">
          <el-pagination
            :page-sizes="[10, 20, 30]"
            :page-size="recordFrom.limit"
            :current-page="recordFrom.page"
            layout="total, sizes, prev, pager, next, jumper"
            :total="recordData.total"
            @size-change="handleSizeChange1"
            @current-change="pageChange1"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  deliveryRecordListApi,
  deliveryRecordDetailApi,
  deliveryRecordImportApi,
} from "@/api/order";
export default {
  name: "DeliveryList",
  data() {
    return {
      fileVisible: false,
      dialogVisible: false,
      loading: false,
      timeVal: [],
      deliveryStatusList: [
        { label: this.$t("all"), value: "" },
        { label: this.$t("completeall"), value: 1 },
        { label: this.$t("partiallycompleted"), value: 10 },
        { label: this.$t("processing"), value: 0 },
        { label: this.$t("processingfailed"), value: -1 },
      ],
      deliveryTypeList: [
        { label: this.$t("all"), value: "" },
        { label: this.$t("delivergoods"), value: 1 },
        { label: this.$t("delivery"), value: 2 },
        { label: this.$t("nologisticsrequired"), value: 3 },
        { label: this.$t("electronicsheet"), value: 4 },
      ],
      tableData: {
        data: [],
        total: 0,
      },
      recordData: {
        data: [],
        total: 0,
      },
      recordFrom: {
        page: 1,
        limit: 20,
      },
      tableFrom: {
        page: 1,
        limit: 10,
        date: "",
        status: "",
      },
      recordId: "",
    };
  },
  methods: {
    getList(num) {
      this.loading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      deliveryRecordListApi(this.tableFrom)
        .then((res) => {
          this.fileVisible = true;
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.loading = false;
        })
        .catch((res) => {
          this.$message.error(res.message);
          this.listLoading = false;
        });
    },
    // 下载
    downLoad(id) {
      deliveryRecordImportApi(id)
        .then((res) => {
          const h = this.$createElement;
          this.$msgbox({
            title: this.$t("tips"),
            message: h("p", null, [
              h("span", null, this.$t("please71")),
              h("span", { style: "color: teal" }, this.$t("exportrecords")),
              h("span", null, this.$t("see")),
            ]),
            confirmButtonText: this.$t("igotit"),
          }).then((action) => {});
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 详情
    detail(id) {
      this.recordId = id;
      deliveryRecordDetailApi(id, this.recordFrom)
        .then((res) => {
          this.dialogVisible = true;
          this.recordData.data = res.data.list;
          this.recordData.total = res.data.count;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      this.getList(1);
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    },
    pageChange1(page) {
      this.recordFrom.page = page;
      this.getList("");
      this.detail(this.recordId);
    },
    handleSizeChange1(val) {
      this.recordFrom.limit = val;
      this.getList("");
      this.detail(this.recordId);
    },
  },
};
</script>

<style scoped lang="scss">
.title {
  margin-bottom: 16px;
  color: #17233d;
  font-weight: 500;
  font-size: 14px;
}
.description {
  &-term {
    display: table-cell;
    padding-bottom: 10px;
    line-height: 20px;
    width: 50%;
    font-size: 12px;
  }
}
</style>
